﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Controls.Primitives;
using System.Windows.Threading;
using Microsoft.Surface.Presentation;

namespace LetsGoToSchool
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class ParentControl : UserControl
    {
        private Main _main;
        private DispatcherTimer _visibilityTimer;
        private int _numContacts;

        public ParentControl()
        {
            InitializeComponent();
            _visibilityTimer = new DispatcherTimer();
            _visibilityTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            _visibilityTimer.Interval = TimeSpan.FromSeconds(20);

            Contacts.AddPreviewContactDownHandler(_closeArrow, rectContactDown);
            Contacts.AddPreviewContactDownHandler(_sideRect, rectContactDown);
            Contacts.AddContactLeaveHandler(_flash, rectContactLeave);
            Contacts.AddContactUpHandler(_flash, rectContactUp);
            Contacts.AddPreviewContactDownHandler(_mainGrid, gridContactDown);
            Contacts.AddContactLeaveHandler(_mainGrid, gridContactLeave);

            _imageManagerButton.Background = new ImageBrush(Utils.getIcon(ImageManager.getIconFileName()));
            _progressReportButton.Background = new ImageBrush(Utils.getIcon(ProgressReport.getIconFileName()));
        }

        public void Show()
        {
            _numContacts = 0;
            this.Visibility = Visibility.Visible;
            _visibilityTimer.Start();
        }

        public void Hide()
        {
            _visibilityTimer.Stop();
            this.Visibility = Visibility.Hidden;
            _main.ParentControlHidden(); ;
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            _visibilityTimer.Stop();
            this.Hide();
        }

        private void gridContactDown(object sender, ContactEventArgs e)
        {
            _numContacts++;
            _visibilityTimer.Stop();
        }

        private void gridContactLeave(object sender, ContactEventArgs e)
        {
            _numContacts--;
            if (_numContacts == 0 && this.IsVisible)
            {
                _visibilityTimer.Start();
            }
        }

        private void rectContactDown(object sender, ContactEventArgs e)
        {
            _flash.Visibility = Visibility.Visible;
        }

        private void rectContactLeave(object sender, ContactEventArgs e)
        {
            _flash.Visibility = Visibility.Hidden;
        }

        private void rectContactUp(object sender, ContactEventArgs e)
        {
            this.Hide();
        }

        private void progress_Click(object sender, RoutedEventArgs e)
        {
            (new ProgressReport()).Show();
        }

        private void upload_Click(object sender, RoutedEventArgs e)
        {
            (new ImageManager()).Show();
        }

        public void setMain(Main main)
        {
            _main = main;
        }
    }
}
